﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team (cellcloudproject@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellBlockingQueue_h
#define CellBlockingQueue_h

#include "CellPrerequisites.h"

namespace cell
{
/** 线程安全的模板阻塞队列。
@author
	Jiangwei Xu
*/
template<typename T>
class BlockingQueue : public GeneralAlloc
{
public:
	/**
	*/
	BlockingQueue(void);

	/**
	*/
	BlockingQueue(size_t capability);

	/**
	*/
	BlockingQueue(size_t capability, bool uniqueness);

	/**
	*/
	virtual ~BlockingQueue(void);

public:
	/**
	*/
	void put(T element);

	/**
	*/
	T take(void);

	/**
	*/
	size_t size(void);

	/**
	*/
	size_t capability(void);

	/**
	*/
	T front(void);

	/**
	*/
	T back(void);

	/**
	*/
	void clear(void);

	/**
	*/
	void resize(size_t capability);

protected:
	typedef std::vector<T> Collection;
	Collection mCollection;
	size_t mCapability;

	bool mUniqueness;

	CELL_MUTEX(mMonitor);
	CELL_THREAD_SYNCHRONISER(mQueueNotFull);
	CELL_THREAD_SYNCHRONISER(mQueueNotEmpty);
};

//
// Implemention
//

//-----------------------------------------------------------------------
template<typename T>
BlockingQueue<T>::BlockingQueue(void) : mCapability(10), mUniqueness(false)
{
}
//-----------------------------------------------------------------------
template<typename T>
BlockingQueue<T>::BlockingQueue(size_t capability) : mCapability(capability),
	mUniqueness(false)
{
}
//-----------------------------------------------------------------------
template<typename T>
BlockingQueue<T>::BlockingQueue(size_t capability, bool uniqueness)
	: mCapability(capability), mUniqueness(uniqueness)
{
}
//-----------------------------------------------------------------------
template<typename T>
BlockingQueue<T>::~BlockingQueue(void)
{
}
//-----------------------------------------------------------------------
template<typename T>
void BlockingQueue<T>::put(T element)
{
	CELL_LOCK_MUTEX_NAMED(mMonitor, lock);
	if (mUniqueness)
	{
		Collection::const_iterator iter = std::find(mCollection.begin(), mCollection.end(), element);
		if (iter != mCollection.end())
		{
			return;
		}
	}
	while (mCollection.size() == mCapability)
	{
		CELL_THREAD_WAIT(mQueueNotFull, mutex, lock);
	}
	mCollection.push_back(element);
	CELL_THREAD_NOTIFY_ONE(mQueueNotEmpty);
}
//-----------------------------------------------------------------------
template<typename T>
T BlockingQueue<T>::take(void)
{
	CELL_LOCK_MUTEX_NAMED(mMonitor, lock);
	while (mCollection.size() == 0)
	{
		CELL_THREAD_WAIT(mQueueNotEmpty, mutex, lock);
	}
	Collection::iterator iter = mCollection.begin();
	T& element = (*iter);
	mCollection.erase(iter);
	CELL_THREAD_NOTIFY_ONE(mQueueNotFull);
	return element;
}
//-----------------------------------------------------------------------
template<typename T>
size_t BlockingQueue<T>::size(void)
{
	CELL_LOCK_MUTEX(mMonitor);
	return mCollection.size();
}
//-----------------------------------------------------------------------
template<typename T>
size_t BlockingQueue<T>::capability(void)
{
	CELL_LOCK_MUTEX(mMonitor);
	return mCapability;
}
//-----------------------------------------------------------------------
template<typename T>
T BlockingQueue<T>::front(void)
{
	CELL_LOCK_MUTEX(mMonitor);
	return mCollection.front();
}
//-----------------------------------------------------------------------
template<typename T>
T BlockingQueue<T>::back(void)
{
	CELL_LOCK_MUTEX(mMonitor);;
	return mCollection.back();
}
//-----------------------------------------------------------------------
template<typename T>
void BlockingQueue<T>::clear(void)
{
	CELL_LOCK_MUTEX(mMonitor);
	mCollection.clear();
}
//-----------------------------------------------------------------------
template<typename T>
void BlockingQueue<T>::resize(size_t capability)
{
	CELL_LOCK_MUTEX(mMonitor);
	mCapability = capability;
}

} // end namespace cell

#endif // CellBlockingQueue_h
